<?php

/**
 * uniBoard ACP Send Mass Mail
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-20 21:23:27 +0000 (Mon, 20 Sep 2010) $
 * $Revision: 64 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_send_mass_mail{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	/**
	 * Our api
	 *
	 * @var object
	 */
	
	private $api			= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Get Groups
		$this -> groups = $appCore -> users -> getUserGroups();
		
		// Create API
		$this -> api = new api_mass_mail( $appCore, $appCore -> _REQ('mail'));
		
		// Api found?
		if ( !$this -> api -> data_loaded)
		{
			// Draw Error
			$appCore -> output -> error( $appCore -> lang['acp_mail_not_found'], $appCore -> lang['acp_mass_mail']);
			
			// Jump back to Tasks List
			new action_mass_mail( $appCore);
		}
		else if ( $appCore -> _REQ('do') == 'send')
		{
			// Send E-Mails
			$this -> sendMails();
		}
		else
		{
			// Draw Form
			$this -> drawForm();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Draws Form
	 *
	 */
	
	function drawForm(){
	
		// Load fields templates
		$this -> appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
				
		// Cache output
		ob_start();
				
		// Open Form
		echo $this -> appCore -> output -> openForm( $this -> appCore -> adminLink( 'act=send_mass_mail&do=send&mail=' . $this -> api -> mail_id));
		echo $this -> appCore -> drawHidden( 'update', 1);
		echo $this -> appCore -> output -> openFormBlock( htmlspecialchars( $this -> api -> mail_subject));
		
		// Draw fields
		echo $this -> appCore -> output -> tableRow( '<div class="cell_pad">' . $this -> appCore -> lang['acp_mail_form_send_information'] . '</div>');
						
		// Close Form
		echo $this -> appCore -> output -> submitButton( $this -> appCore -> lang['send']);
		echo $this -> appCore -> output -> closeFormBlock();
		echo $this -> appCore -> output -> closeForm();
		
		// Draw output
		$this -> appCore -> output -> draw( $this -> appCore -> lang['acp_send_mail_form'], array(
			$this -> appCore -> adminLink( 'act=mass_mail') => $this -> appCore -> lang['acp_mass_mail'],
			'' => htmlspecialchars( $this -> api -> mail_subject)
		), ob_get_clean());
			
	}

	// ------------------------------------------------------------------------

	/**
	 * Send Mails
	 *
	 */
	
	function sendMails(){
		
		// Stop software from timing out or being aborted
		ignore_user_abort( true);
		
		// Sent Mails counter
		$sent_mails = 0;
				
		// Bring PHP-Mailer to life
		$php_mailer = new class_phpmailer();
		
		// Are we using SMTP?
		if ( $this -> appCore -> conf['board_email_use_smtp'])
		{
			// Use SMTP
			$php_mailer -> IsSMTP();
						
			// Use Auth
			$php_mailer -> SMTPAuth 	= true;
			
			// Host and Port
			$php_mailer -> Host 		= $this -> appCore -> conf['board_email_smtp_host'];
			$php_mailer -> Port 		= $this -> appCore -> conf['board_email_smtp_port'];
			
			// User Name and Password
			$php_mailer -> Username 	= $this -> appCore -> conf['board_email_smtp_user_name'];
			$php_mailer -> Password 	= $this -> appCore -> conf['board_email_smtp_user_pass'];
		}
		else
		{
			// Tell Mailer to use mail()
			$php_mailer -> IsMail();
		}

		// Tell it to send tons of emails
		$php_mailer -> SMTPKeepAlive = true;
			
		// Set Board data
		$php_mailer -> SetFrom( $this -> appCore -> conf['board_outcoming_email'], $this -> appCore -> conf['board_name'] . ( isset($this -> appCore -> conf['board_description'][0]) ? ' - ' . $this -> appCore -> conf['board_description'] : ''));
		$php_mailer -> AddReplyTo( $this -> appCore -> conf['board_incoming_email'], $this -> appCore -> conf['board_name'] . ( isset($this -> appCore -> conf['board_description'][0]) ? ' - ' . $this -> appCore -> conf['board_description'] : ''));
		
		// Set New Recipient
		$php_mailer -> AddAddress( $this -> appCore -> conf['board_outcoming_email'], $this -> appCore -> conf['board_name'] . ( isset($this -> appCore -> conf['board_description'][0]) ? ' - ' . $this -> appCore -> conf['board_description'] : ''));
		
		// Give Mail to Mailer
		$php_mailer -> Subject 		= $this -> api -> mail_subject;
		$php_mailer -> Body			= $this -> api -> mail_content;
		
		// Append Board's Signature?
		if ( isset( $this -> appCore -> conf['board_email_signature'][0]))
		{
			// Append it
			$php_mailer -> Body .= "\n\n\n-------------------------------------------------------------------------------------------------------\n" . $this -> appCore -> conf['board_email_signature'];
		}
		
		// Select Users
		$this -> appCore -> db -> query( 'SELECT user_name, user_email
		FROM users
		WHERE user_id > \'0\' AND group_id IN (' . (empty( $this -> api -> mail_groups) ? '0' : class_strings::stripBrackets( $this -> appCore -> db -> strEscape( join( ',', $this -> api -> mail_groups)))) . ')' . ($this -> api -> mail_everyone ? '' : ' AND user_want_mailings = \'1\''));
		
		// Parse Users
		while ( $user_result = $this -> appCore -> db -> fetch_array())
		{
			// Add BCC
			$php_mailer -> AddBCC( $user_result[1], $user_result[0]);
			
			// Increase stats
			$sent_mails ++;
			
			// Package size hit?
			if ( $sent_mails == $this -> appCore -> conf['board_email_package_size'])
			{
				// Reset Counter
				$sent_mails = 0;
				
				// Send Mailing
				$php_mailer -> Send();
				
				// Clear BCC's
				$php_mailer -> ClearBCCs();
			}
		}
		
		// Send Mail?
		if ( $sent_mails > 0)
		{
			$php_mailer -> Send();	
		}
		
		// Close SMPT?
		$php_mailer -> SmtpClose();
		
		// Send output to user?
		if ( connection_aborted() == 0)
		{
			// Set Log
			$this -> appCore -> logAdmin( 'log_admin_sent_mass_mail', htmlspecialchars( $this -> api -> mail_subject));
			
			// Redirect us back to list
			$this -> appCore -> output -> drawRedirect( $this -> appCore -> adminLink( 'act=mass_mail'), $this -> appCore -> lang['acp_mail_sent']);
		}
		
	}
	
}